<?php
namespace app\admin\controller;
use app\common\letterbusiness\Letter as letterBn;
use think\Db;
class Letter extends Common{
    protected  $dao,$fields,$categorys;
    public function _initialize()
    {
        parent::_initialize();

        $this->dao = db('letter_imgs');

        unset($fields);
        unset($res);
        $this->assign ('fields',$this->fields);
    }
    public function index(){

        if(request()->isPost()){

            $model = db('letter_imgs');
            $status=input('post.status');
            $id=input('post.key');
            $page =input('page')?input('page'):1;
            $pageSize =input('limit')?input('limit'):config('pageSize');

            if(!empty($keyword) ){
                $map['title']=array('like','%'.$keyword.'%');
            }

            if(!empty($id) ){
                $map['id']=$id;

            }
//            if(isset($status)){
//                if($status==2)
//                    $map['status']=['not in','2'];
//                else
//                $map['status']=$status;
//            }else{
//                $map['status']=['not in','2'];
//            }
            if(!empty($catid)&&empty($tagArc)){
                $list['data']=array();
                $list['total']=0;
            }else{
                $list = $model
                    ->where($map)
                    ->paginate(array('list_rows'=>$pageSize,'page'=>$page))
                    ->toArray();

                $lists = $list['data'];
                foreach ($lists as $k=>$v ){

                    $lists[$k]['status'] = $v['status']?'显示':'隐藏';
                }
                $rsult['data'] = $lists;
                $rsult['count'] = $list['total'];


            }
            $rsult['msg'] = "获取成功";
            $rsult['code'] = 0;
            $rsult['rel'] = 1;
            return $rsult;
        }else{
            return $this->fetch ();
        }
    }

    //添加编辑文章
    public function editArticle() {
        $id = request()->param('id');
        $model = db('letter_imgs');
        if (request()->isPost()) {

            $data = input('');
            $result = letterBn::save($data, $id);
            if ($result) {
                return json(['code' => 1, 'msg' => '操作成功!', 'url' => url('index')]);
            } else {
                return json(['code' => 0, 'msg' => '操作失败!']);
            }
        } else {
            $map['id']=$id;
            $info=$model->where($map)->find();//转入回收站
            //文章标签
            $info['photos']=json_decode($info['photo_imgs'],true);

            $this->assign('info', $info);
            //文章属性
            $this->assign('title', '添加/修改内容');
            return $this->fetch();
        }
    }

    public function listDel(){
        $id = input('post.id');
        $model = $this->dao;
        $model->where(array('id'=>$id))->update(array('status'=>2));//转入回收站
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function del(){
        $id = input('post.id');
        $model = db('letter_imgs');
        $model->where(array('id'=>$id))->delete();
        return ['code'=>1,'msg'=>'删除成功！'];
    }
    public function delAll(){
        $map[] = array('id', 'IN', input('param.ids/a'));
        $model =  db('letter_imgs');
        $model->where($map)->delete();
        $result['code'] = 1;
        $result['msg'] = '删除成功！';
        $result['url'] = url('index');
        return $result;
    }
    public function showAll(){
        $map['id'] =array('in',input('param.ids/a'));
        $model = $this->dao;
        $model->where($map)->update(array('status'=>1));
        $result['code'] = 1;
        $result['msg'] = '显示成功！';
        $result['url'] = url('index',array('catid'=>input('post.catid')));
        return $result;
    }
    public function hiddenAll(){
        $map['id'] =array('in',input('param.ids/a'));
        $model = $this->dao;
        $model->where($map)->update(array('status'=>0));
        $result['code'] = 1;
        $result['msg'] = '隐藏成功！';
        $result['url'] = url('index',array('catid'=>input('post.catid')));
        return $result;
    }
    public function listorder(){
        $model = $this->dao;
        $catid = input('catid');
        $data = input('post.');
        $model->update($data);
        $result = ['msg' => '排序成功！','url'=>url('index',array('catid'=>$catid)), 'code' => 1];
        return $result;
    }
    public function delImg(){
        if(!input('post.url')){
            return ['code'=>0,'请指定要删除的图片资源'];
        }
        $file = ROOT_PATH.__PUBLIC__.input('post.url');
        if(file_exists($file) && trim(input('post.url'))!=''){
            is_dir($file) ? dir_delete($file) : unlink($file);
        }
        if(input('post.id')){
            $picurl = input('post.picurl');
            $picurlArr = explode(':',$picurl);
            $pics = substr(implode(":::",$picurlArr),0,-3);
            $model = $this->dao;
            $map['id'] =input('post.id');
            $model->where($map)->update(array('pics'=>$pics));
        }
        $result['msg'] = '删除成功!';
        $result['code'] = 1;
        return $result;
    }
    public function getRegion(){
        $Region=db("region");
        $map['pid'] = input("pid");
        $list=$Region->where($map)->select();
        return $list;
    }


}